package com.zhuqi.controller;

import com.zhuqi.bloomfilter.BloomFilterCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author : ZhuQi
 * @version : 时间:2022/1/7 9:25 版本:
 */
@RestController
public class UserCodeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取用户健康码接口
     * 使用Redis缓存
     * @param
     * @return
     */
    @GetMapping("getUserCode/{id}")
    @BloomFilterCode
    public String method(@PathVariable("id")String id) {
        String userCode = stringRedisTemplate.opsForValue().get(id);
        if (StringUtils.hasText(userCode)) {
            return userCode;
        }
        stringRedisTemplate.opsForValue().set(id, "二维码", 10L, TimeUnit.SECONDS);
        return new String("二维码");

    }
}
